package com.gzy.rpsm_property.mapper;

import com.github.pagehelper.Page;
import com.gzy.rpsm_property.pojo.Pay;
import com.gzy.rpsm_property.pojo.PayInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;

@Repository
public interface PayMapper {
@Select("<script>" +
        "SELECT payInfo.pay_id, payInfo.building_id, payInfo.room_id, u.userid " +
        "FROM (" +
        "    SELECT p.pay_id, b.building_id, r.room_id, f.floor_id " +
        "    FROM (" +
        "        SELECT pay.pay_id, pay.pay_room_id " +
        "        FROM pay " +
        "        <where> " +
        "            <if test='start != null and end != null'> " +
        "                pay.pay_date BETWEEN #{start} AND #{end} " +
        "            </if> " +
        "        </where> " +
        "    ) p " +
        "    JOIN building b ON p.pay_room_id IN (" +
        "        SELECT r.room_id " +
        "        FROM room r " +
        "        <where> " +
        "            <if test='floorId != null'> " +
        "                r.room_floor_id = #{floorId} " +
        "            </if> " +
        "        </where> " +
        "    ) " +
        "    JOIN room r ON r.room_id = p.pay_room_id " +
        "    JOIN floor f ON r.room_floor_id = f.floor_id " +
        "    WHERE f.belong_building = b.building_id " +
        "    <if test='buildingId != null and floorId == null'> " +
        "        AND f.belong_building = #{buildingId} " +
        "    </if> " +
        ") payInfo " +
        "LEFT JOIN (" +
        "    SELECT userid, user_roomid " +
        "    FROM `user` " +
        "    <where> " +
        "        <if test='username != null and username != \"\"'> " +
        "            username LIKE CONCAT('%', #{username}, '%') " +
        "        </if> " +
        "        <if test='idCard != null and idCard != \"\"'> " +
        "            AND idcard LIKE CONCAT('%', #{idCard}, '%') " +
        "        </if> " +
        "    </where> " +
        ") u ON u.user_roomid = payInfo.room_id " +
        "</script>")

@Results(id="payInfo",value = {
            @Result(property = "user",column = "userid",one =@One(select = "com.gzy.rpsm_property.mapper.UserMapper.queryById")),
            @Result(property = "building",column = "building_id",one = @One(select="com.gzy.rpsm_property.mapper.BuildingMapper.queryById")),
            @Result(property = "room",column = "room_id",one = @One(select="com.gzy.rpsm_property.mapper.RoomMapper.roomById")),
            @Result(property = "pay",column = "pay_id",one = @One(select="com.gzy.rpsm_property.mapper.PayMapper.queryById")),
    })
            Page<PayInfo> selectPayInfoOnCondition(
                    @Param("start") LocalDate start,
                    @Param("end")LocalDate end,
                    @Param("username")String username,
                    @Param("idCard")String idCard,
                    @Param("floorId")Integer floorId,
                    @Param("buildingId")Integer buildingId
            );
    @Select("<script>" +
            "select * from pay where pay_id = #{id}" +
            "</script>")

    Pay queryById(Integer id);


}
